// ignore_for_file: constant_identifier_names

import 'package:date_format/date_format.dart';

///
///
///Dart的日期时间
///
///https://api.dart.cn/stable/2.16.1/dart-core/DateTime-class.html
///
///

String dateTimeStringChange(String? dateTimeStr,
    {List<String> formats = const <String>[
      yyyy,
      '-',
      mm,
      '-',
      dd,
      ' ',
      HH,
      ':',
      nn,
      ':',
      ss
    ]}) {
  if (dateTimeStr != null) {
    DateTime? data = DateTime.tryParse(dateTimeStr);
    if (data != null) {
      return dateTimeToString(data, formats: formats);
    }
  }
  return "";
}

String dateTimeToString(
  DateTime dateTime, {
  List<String> formats = const <String>[
    yyyy,
    '-',
    mm,
    '-',
    dd,
    ' ',
    HH,
    ':',
    nn,
    ':',
    ss
  ],
}) {
  return formatDate(dateTime, formats);
}

///通过指定年月日时分秒的方式
///转换为'yyyy-MM-dd HH:mm:ss'格式日期时间
///
String toZhCNLocalDateTime(
  int year, [
  int month = 1,
  int day = 1,
  int hour = 0,
  int minute = 0,
  int second = 0,
]) {
  var dateTime = formatDate(
      DateTime(
        year,
        month,
        day,
        hour,
        minute,
        second,
      ),
      [yyyy, '-', mm, '-', dd, ' ', HH, ':', nn, ':', ss]);
  return dateTime;
}

///
///通过指定年月日的方式
///转换为'yyyy-MM-dd'格式日期
///
String toZhCNLocalDate(
  int year, [
  int month = 1,
  int day = 1,
]) {
  var dateTime = formatDate(
      DateTime(
        year,
        month,
        day,
      ),
      [yyyy, '-', mm, '-', dd]);
  return dateTime;
}

//转换成毫秒
int parseMilliSeconds(String? datetime) {
  if (datetime != null && datetime.isNotEmpty) {
    return DateTime.parse(datetime).millisecondsSinceEpoch;
  }
  return 0;
}

///
///通过DateTime的方式
///转换为'yyyy-MM-dd HH:mm:ss'格式日期时间
///
String toLocalDateTimeByDateTime(DateTime dateTime) {
  return toZhCNLocalDateTime(
    dateTime.year,
    dateTime.month,
    dateTime.day,
    dateTime.hour,
    dateTime.minute,
    dateTime.second,
  );
}

String? toNullableLocalDateTimeByDateTime(dynamic dateTime) {
  if (dateTime is DateTime) {
    return toZhCNLocalDateTime(
      dateTime.year,
      dateTime.month,
      dateTime.day,
      dateTime.hour,
      dateTime.minute,
      dateTime.second,
    );
  }
  return null;
}

///
///通过DateTime的方式
///转换为'yyyy-MM-dd'格式日期时间
///
String toLocalDateByDateTime(DateTime dateTime) {
  return toZhCNLocalDate(
    dateTime.year,
    dateTime.month,
    dateTime.day,
  );
}

///
///转换DateTime
///
DateTime parseDateTime(String dateTime) {
  return DateTime.parse(dateTime);
}

///
///转换String Date
///
String parseStringDate(String? dateTime) {
  if (dateTime == null || dateTime.isEmpty) {
    return "";
  }
  return toLocalDateByDateTime(DateTime.parse(dateTime));
}

///获取当前时间
DateTime now() {
  return DateTime.now();
}

///
///比较时间早晚
///
///a < b (早于) 返回 -1
///a = b (等于) 返回 0
///a > b (晚于) 返回 1
///
int compare(DateTime a, DateTime b) {
  return a.compareTo(b);
}

///后推时间
///
DateTime add(DateTime datetime, Duration duration) {
  return datetime.add(duration);
}

///前推时间
///
DateTime subtract(DateTime datetime, Duration duration) {
  return datetime.subtract(duration);
}

///
///后推时间
///
DateTime addDuration(
  DateTime datetime, {
  int days = 0,
  int hours = 0,
  int minutes = 0,
  int seconds = 0,
}) {
  return add(
      datetime,
      Duration(
        days: days,
        hours: hours,
        minutes: minutes,
        seconds: seconds,
      ));
}

///
///前推时间
///
DateTime substractDuration(
  DateTime datetime, {
  int days = 0,
  int hours = 0,
  int minutes = 0,
  int seconds = 0,
}) {
  return subtract(
      datetime,
      Duration(
        days: days,
        hours: hours,
        minutes: minutes,
        seconds: seconds,
      ));
}

///
/// 相对友好时间格式
///
class RelativeDateFormat {
  static const num ONE_MINUTE = 60000;
  static const num ONE_HOUR = 3600000;
  static const num ONE_DAY = 86400000;
  static const num ONE_WEEK = 604800000;

  static const String ONE_SECOND_AGO = "秒前";
  static const String ONE_MINUTE_AGO = "分钟前";
  static const String ONE_HOUR_AGO = "小时前";
  static const String ONE_DAY_AGO = "天前";
  static const String ONE_MONTH_AGO = "月前";
  static const String ONE_YEAR_AGO = "年前";

//时间转换
  static String format(DateTime date) {
    num delta =
        DateTime.now().millisecondsSinceEpoch - date.millisecondsSinceEpoch;
    if (delta < 1 * ONE_MINUTE) {
      num seconds = toSeconds(delta);
      return (seconds <= 0 ? 1 : seconds).toInt().toString() + ONE_SECOND_AGO;
    }
    if (delta < 45 * ONE_MINUTE) {
      num minutes = toMinutes(delta);
      return (minutes <= 0 ? 1 : minutes).toInt().toString() + ONE_MINUTE_AGO;
    }
    if (delta < 24 * ONE_HOUR) {
      num hours = toHours(delta);
      return (hours <= 0 ? 1 : hours).toInt().toString() + ONE_HOUR_AGO;
    }
    if (delta < 48 * ONE_HOUR) {
      return "昨天";
    }
    if (delta < 30 * ONE_DAY) {
      num days = toDays(delta);
      return (days <= 0 ? 1 : days).toInt().toString() + ONE_DAY_AGO;
    }
    if (delta < 12 * 4 * ONE_WEEK) {
      num months = toMonths(delta);
      return (months <= 0 ? 1 : months).toInt().toString() + ONE_MONTH_AGO;
    } else {
      num years = toYears(delta);
      return (years <= 0 ? 1 : years).toInt().toString() + ONE_YEAR_AGO;
    }
  }

  static num toSeconds(num date) {
    return date / 1000;
  }

  static num toMinutes(num date) {
    return toSeconds(date) / 60;
  }

  static num toHours(num date) {
    return toMinutes(date) / 60;
  }

  static num toDays(num date) {
    return toHours(date) / 24;
  }

  static num toMonths(num date) {
    return toDays(date) / 30;
  }

  static num toYears(num date) {
    return toMonths(date) / 365;
  }
}
